package com.rul.mars.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rul.mars.common.constant.DeleteStatusConstant;
import com.rul.mars.common.exception.Asserts;
import com.rul.mars.mbg.domain.ProductCategoryNode;
import com.rul.mars.mbg.domain.ProductCategoryParam;
import com.rul.mars.mbg.mapper.ProductCategoryMapper;
import com.rul.mars.mbg.model.ProductCategory;
import com.rul.mars.mbg.model.ProductCategoryExample;
import com.rul.mars.product.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author RuL
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper categoryMapper;

    /**
     * 查询三级分类信息并封装为树形结构
     *
     * @return 三级分类结构
     */
    @Override
    public List<ProductCategoryNode> categoryStructure() {
        ProductCategoryExample example = new ProductCategoryExample();
        List<ProductCategory> productCategories = categoryMapper.selectByExample(example);

        return productCategories.stream()
                //过滤出所有一级分类
                .filter(item -> item.getParentId().equals(0L))
                //转化为ProductCategoryNode
                .map(item -> covert(item, productCategories))
                //转为List
                .collect(Collectors.toList());
    }

    /**
     * 修改分类信息
     *
     * @param id    分类ID
     * @param param 修改参数
     * @return 修改结果
     */
    @Override
    public boolean updateCategory(Long id, ProductCategoryParam param) {
        ProductCategory category = new ProductCategory();
        category.setId(id);
        //将修改后的属性复制到category中
        BeanUtil.copyProperties(param, category);
        int rows = categoryMapper.updateByPrimaryKeySelective(category);
        return rows == 1;
    }

    /**
     * 删除商品分类
     *
     * @param id 分类ID
     * @return 删除结果
     */
    @Override
    public boolean deleteCategory(Long id) {
        ProductCategory category = categoryMapper.selectByPrimaryKey(id);
        //设置删除状态为1
        category.setDeleteStatus(DeleteStatusConstant.YES);
        int rows = categoryMapper.updateByPrimaryKeySelective(category);
        return rows == 1;
    }

    /**
     * 创建商品分类
     *
     * @param param 商品分类相关参数
     * @return 创建结果
     */
    @Override
    public boolean createCategory(ProductCategoryParam param) {
        ProductCategory parent = categoryMapper.selectByPrimaryKey(param.getParentId());
        if ((parent != null && parent.getLevel() + 1 != param.getLevel())
                || (parent == null && param.getLevel() != 1)) {
            //分类级别必须为父级分类的分类级别+1
            Asserts.fail("分类级别不正确");
        }
        ProductCategory category = BeanUtil.copyProperties(param, ProductCategory.class);
        int rows = categoryMapper.insertSelective(category);
        return rows == 1;
    }

    /**
     * 根据商品ID查询分类信息及其子分类信息
     *
     * @param id 分类ID
     * @return 分类信息
     */
    @Override
    public ProductCategoryNode getInfo(Long id) {
        //查询该分类
        ProductCategory category = categoryMapper.selectByPrimaryKey(id);
        //封装为node
        ProductCategoryNode categoryNode = BeanUtil.copyProperties(category, ProductCategoryNode.class);

        //查询子分类
        ProductCategoryExample example = new ProductCategoryExample();
        example.createCriteria()
                .andParentIdEqualTo(category.getId())
                .andDeleteStatusEqualTo(DeleteStatusConstant.NO);
        List<ProductCategory> children = categoryMapper.selectByExample(example);

        //转化为node
        List<ProductCategoryNode> childNodes = children.stream()
                .map(item -> BeanUtil.copyProperties(item, ProductCategoryNode.class))
                .collect(Collectors.toList());

        categoryNode.setChildren(childNodes);
        return categoryNode;
    }

    /**
     * 将ProductCategory转化为ProductCategoryNode
     *
     * @param category          分类信息
     * @param productCategories 所有分类信息
     * @return 转化为ProductCategoryNode的分类信息
     */
    public ProductCategoryNode covert(ProductCategory category, List<ProductCategory> productCategories) {
        //复制基本属性
        ProductCategoryNode categoryNode = BeanUtil.copyProperties(category, ProductCategoryNode.class);

        List<ProductCategoryNode> children = productCategories.stream()
                //找出子分类
                .filter(item -> item.getParentId().equals(category.getId()))
                //子分类递归转化为ProductCategoryNode
                .map(item -> covert(item, productCategories))
                .collect(Collectors.toList());
        categoryNode.setChildren(children);

        return categoryNode;
    }


}
